package com.bdwm.controller;

import java.io.IOException;
import java.util.List;
import javax.print.DocFlavor.READER;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.bdwm.dao.DeviveDBService;
import com.bdwm.dao.UserDBService;
import com.bdwm.model.Constants;
import com.bdwm.model.Device;
import com.bdwm.util.ParseRequestPara;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class DeviceManageController {
	
	/**
	 * 
	 * @Description：获取设备信息列表
		 * @author：wangwenhui
		 * @time：2017年11月27日下午3:04:29
	 */
	@RequestMapping(value = "/getAllDevice", method = RequestMethod.GET)
	public void getDeviceList(HttpServletResponse response,HttpServletRequest request) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		List<Device> deviceList = DeviveDBService.getAllDeviceInfo();
		JSONObject json = new JSONObject();
		json.accumulate("status", 0);
		JSONArray jsonArr = new JSONArray();
		for(Device device: deviceList) {
			JSONObject deviceJson = new JSONObject();
			deviceJson.accumulate("deviceName",device.getDeviceName());
			deviceJson.accumulate("deviceAssetNum",device.getDeviceAssetNum());
			deviceJson.accumulate("osType",device.getOsType());
			deviceJson.accumulate("versionType",device.getVersionType());
			deviceJson.accumulate("deviceResolution",device.getDeviceResolution());
			deviceJson.accumulate("remark",device.getRemark());
			deviceJson.accumulate("deviceDept",device.getDeviceDept());
			deviceJson.accumulate("deviceBelongs",device.getDeviceBelongs());
			jsonArr.add(deviceJson);
		}
		json.accumulate("data", jsonArr);
		
		response.getWriter().println(json.toString());
		response.getWriter().close();
	}
	
	/**
	 * 
		 * @throws IOException 
		 * @Description：删除设备信息
		 * @author：wangwenhui
		 * @time：2017年11月28日下午2:29:41
	 */
	@RequestMapping(value = "/deleteDevice", method = RequestMethod.GET)
	public void deleteDeviceInfo(HttpServletResponse response,HttpServletRequest request) throws IOException {
		ServletOutputStream output = response.getOutputStream();
		request.setCharacterEncoding("utf-8");
		String deviceName = ParseRequestPara.parseRequestParameter(request.getParameter("deviceName"));
		JSONObject json = new JSONObject();
		if(ParseRequestPara.isNull(deviceName)){
			json.accumulate("status", 1);
			json.accumulate("message", "deviceName is null!");
			output.println(json.toString());
			return;
		}
		if(!DeviveDBService.findIfDeviceExist(deviceName)) {
			json.accumulate("status", 1);
			json.accumulate("message", "device not exist!");
			output.println(json.toString());
			return;
		}
		
		int row = DeviveDBService.deleteDeviceInfo(deviceName);
		if(row > 0) {
			json.accumulate("status", 0);
			json.accumulate("message", "delete device success!");
		}else {
			json.accumulate("status", 1);
			json.accumulate("message", "delete device fail!");
		}
		output.println(json.toString());
	}
	
	/**
	 * 
	 * @Description：更新设备
	 * @throws IOException 
	 * @author：wangwenhui
	 * @time：2017年11月28日下午4:44:39
	 */
	@RequestMapping(value = "/updateDevice", method = RequestMethod.GET)
	public void updateDevice(HttpServletResponse response,HttpServletRequest request) throws IOException {
		ServletOutputStream output = response.getOutputStream();
		request.setCharacterEncoding("utf-8");
		Device device  = new Device();
		device.setDeviceName(ParseRequestPara.parseRequestParameter(request.getParameter("deviceName")));
		device.setDeviceAssetNum(ParseRequestPara.parseRequestParameter(request.getParameter("deviceAssetNum")));
		device.setOsType(ParseRequestPara.parseRequestParameter(request.getParameter("osType")));
		device.setVersionType(ParseRequestPara.parseRequestParameter(request.getParameter("versionType")));
		device.setDeviceResolution(ParseRequestPara.parseRequestParameter(request.getParameter("deviceResolution")));
		device.setRemark(ParseRequestPara.parseRequestParameter(request.getParameter("remark")));
		device.setDeviceDept(ParseRequestPara.parseRequestParameter(request.getParameter("deviceDept")));
		device.setDeviceBelongs(ParseRequestPara.parseRequestParameter(request.getParameter("deviceBelongs")));
		String checkPara = ParseRequestPara.checkDeviceParameter(device);
		JSONObject json = new JSONObject();
		if (!"success".equals(checkPara)) {
			json.accumulate("status", 1);
			json.accumulate("message", checkPara);
			output.println(json.toString());
			return;
		}
		//先不做更新设备名称重复的校验
		if(!DeviveDBService.findIfDeviceExist(device.getDeviceName())) {
			json.accumulate("status", 1);
			json.accumulate("message", "device not exist!");
			output.println(json.toString());
			return;
		}
		int row = DeviveDBService.updateDevice(device);
		if(row > 0) {
			json.accumulate("status", 0);
			json.accumulate("message", "update device success!");
		}else {
			json.accumulate("status", 1);
			json.accumulate("message", "update device fail!");
		}
		output.println(json.toString());
	}
	
	/**
	 * 
	 * @throws IOException 
	 * @Description：添加设备
	 * @author：wangwenhui
	 * @time：2017年11月29日上午11:01:35
	 */
	@RequestMapping(value = "/addDevice", method = RequestMethod.GET)
	public void addDevice(HttpServletResponse response,HttpServletRequest request) throws IOException {
		ServletOutputStream output = response.getOutputStream();
		request.setCharacterEncoding("utf-8");
		Device device = new Device();
		device.setDeviceName(ParseRequestPara.parseRequestParameter(request.getParameter("deviceName")));
		device.setDeviceAssetNum(ParseRequestPara.parseRequestParameter(request.getParameter("deviceAssetNum")));
		device.setOsType(ParseRequestPara.parseRequestParameter(request.getParameter("osType")));
		device.setVersionType(ParseRequestPara.parseRequestParameter(request.getParameter("versionType")));
		device.setDeviceResolution(ParseRequestPara.parseRequestParameter(request.getParameter("deviceResolution")));
		device.setRemark(ParseRequestPara.parseRequestParameter(request.getParameter("remark")));
		device.setDeviceDept(ParseRequestPara.parseRequestParameter(request.getParameter("deviceDept")));
		device.setDeviceBelongs(ParseRequestPara.parseRequestParameter(request.getParameter("deviceBelongs")));
		
		String checkPara = ParseRequestPara.checkDeviceParameter(device);
		JSONObject json = new JSONObject();
		if (!"success".equals(checkPara)) {
			json.accumulate("status", 1);
			json.accumulate("message", checkPara);
			output.println(json.toString());
			return;
		}
		
		boolean isExist = DeviveDBService.findIfDeviceExist(device.getDeviceName());
		if(isExist) {
			json.accumulate("status", 1);
			json.accumulate("message", "device already exist!!");
			output.println(json.toString());
			return;
		}
		
		int row = DeviveDBService.addDevice(device);
		if(Constants.SUCCESS == row) {
			json.accumulate("status", 0);
			json.accumulate("message", "add device success!");
		}else {
			json.accumulate("status", 1);
			json.accumulate("message", "add device fail!");
		}
		output.println(json.toString());
	}
	  
}
